package com.ruoyi.web.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.web.utils.IdWorker;
import com.ruoyi.web.vo.ShareConfigVO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.ShareConfig;
import com.ruoyi.web.service.IShareConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分享链接Controller
 *
 * @author ruoyi
 * @date 2023-01-17
 */
@RestController
@RequestMapping("/share/config")
public class ShareConfigController extends BaseController
{
    @Autowired
    IdWorker idWorker;

    @Autowired
    private IShareConfigService shareConfigService;

    @GetMapping("/app/list")
    public R<List<ShareConfigVO>> appList() {
        List<ShareConfig> list = shareConfigService.selectShareConfigList(new ShareConfig());
        List<ShareConfigVO> voList = new ArrayList<>();
        for (ShareConfig config : list) {
            ShareConfigVO vo = new ShareConfigVO();
            BeanUtils.copyProperties(config,vo);
            voList.add(vo);
        }
        return R.ok(voList);
    }

    /**
     * 查询分享链接列表
     */
    @PreAuthorize("@ss.hasPermi('web:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShareConfig shareConfig)
    {
        startPage();
        List<ShareConfig> list = shareConfigService.selectShareConfigList(shareConfig);
        return getDataTable(list);
    }

    /**
     * 导出分享链接列表
     */
    @PreAuthorize("@ss.hasPermi('web:config:export')")
    @Log(title = "分享链接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShareConfig shareConfig)
    {
        List<ShareConfig> list = shareConfigService.selectShareConfigList(shareConfig);
        ExcelUtil<ShareConfig> util = new ExcelUtil<ShareConfig>(ShareConfig.class);
        util.exportExcel(response, list, "分享链接数据");
    }

    /**
     * 获取分享链接详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shareConfigService.selectShareConfigById(id));
    }

    /**
     * 新增分享链接
     */
    @PreAuthorize("@ss.hasPermi('web:config:add')")
    @Log(title = "分享链接", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShareConfig shareConfig)
    {
        shareConfig.setId(idWorker.nextId());
        return toAjax(shareConfigService.insertShareConfig(shareConfig));
    }

    /**
     * 修改分享链接
     */
    @PreAuthorize("@ss.hasPermi('web:config:edit')")
    @Log(title = "分享链接", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShareConfig shareConfig)
    {
        return toAjax(shareConfigService.updateShareConfig(shareConfig));
    }

    /**
     * 删除分享链接
     */
    @PreAuthorize("@ss.hasPermi('web:config:remove')")
    @Log(title = "分享链接", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shareConfigService.deleteShareConfigByIds(ids));
    }
}
